<?php 
	class module_recentPosts extends module
	{
		public $content;
		public function get_content($params='')
		{
			
			if ($params == '')
				$this->recent_posts();
			else {
				$this->recent_posts_with_ids($params);
			}
			
		}
		private function recent_posts()
		{
			$content = '';
			$query = "SELECT p.id,p.created_on,p.title FROM posts as p WHERE p.state=1 ORDER BY p.created_on DESC LIMIT 0,10";
			$result = mysql_query($query) or die(mysql_error());
			$content = $content.'<ul id="recent-posts-wrapper">';
			while ($row = mysql_fetch_array($result)) {
				$date = date_create($row[created_on]);
				$url = BASEURL.'/index.php?option=com_content&view=article&id='.$row[id];
				$content = $content.'<li><a href="'.$url.'">'.$row[title].'&nbsp;('.date_format($date,'d/m/Y').')</a></li>';
			}
			$content = $content.'</ul>';
			echo $content;
		}
		//Get recent posts with ids
		private function recent_posts_with_ids($params)
		{
			$cat = $this->get_cat_name($params);
			$query = "SELECT p.id,p.created_on,p.title,p.intro,p.content FROM posts as p LEFT JOIN categories as c ON p.cat_id=c.id WHERE p.state=1 AND c.state=1 AND c.cat_path LIKE '".$cat[cat_path]."%' ORDER BY p.created_on DESC LIMIT 0,6";
			$result = mysql_query($query) or die(mysql_error());
			
			$url = 'http://localhost/cms/index.php?option=com_content&view=category&layout=blog&id='.$params;
?>
			<div class="content-box">
				<a href="<?php echo $url;?>"><h1><?php echo $cat[category_name];?></h1></a>
<?php
			$first = 0;
			while ($row = mysql_fetch_array($result)) {
				$postUrl = 'http://localhost/cms/index.php?option=com_content&view=article&id='.$row[id];
				$title = '<a href="'.$postUrl.'">'.$row[title].'</a>';
				if ($first == 0) {
					$first++;
					$imgUrl = $this->get_post_image($post[intro].$post[content]);
					if ($imgUrl == '')
						$imgUrl = BASEURL.'/images/nemo.jpg';
					$timthumbUrl = BASEURL.'/images/thumbnail/timthumb.php';
					$imgUrl = $timthumbUrl.'?src='.$imgUrl.'&amp;w=100&amp;h=70&amp;q=80';
?>
					<a href="<?php echo $postUrl;?>"><h2><?php echo $row[title];?></h2></a>
					<img src="<?php echo $imgUrl;?>" alt="post-image" />
					<div class="post-content"><?php echo $row[intro];?></div><!--End .post-content-->
					<ul class="more-link">
<?php
				}
				else {
					$date = date_create($row[created_on]);
?>
						<li><a href="<?php echo $postUrl;?>"><?php echo $row[title].'&nbsp;('.date_format($date,'d/m/Y h:m:s').')';?></a></li>
<?php				
				}
			}
?>			
					</ul>
			</div><!-- End .content-box-->
<?php
		}
		//Get category name
		private function get_cat_name($c_id)
		{
			$query = "SELECT c.cat_path,c.category_name FROM categories as c WHERE c.id=$c_id";
			$result = mysql_query($query) or die(mysql_error());
			if (mysql_affected_rows() > 0) {
				$row = mysql_fetch_array($result);
				return $row;
			}
			return null;
		}
		//Get image link from post
		private function get_post_image($fulltext) {
			preg_match("/\<img.+?src=\"(.+?)\".+?\/>/", $fulltext, $matches);
			if(count($matches)){
				return $matches[1];
			} else {return '';}
		}
	}
?>